using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Plugin.Sns.Scene;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.UserContrl
{
	public class CommentMsgCtrl : SnsHttpImgCtrlBase
	{
		private const double conMaxWidth = 232.0;
		private const double strangerMaxWidth = 198.0;
		private const int MAX_ELEMENT_NUM = 400;
		private SnsMsg snsMsg;
		private SnsInfo snsInfo;
		private TimeLineObject tlObj;
		private List<BitmapImage> btimgList;
		private static List<CommentMsgCtrl> CommentHeadImgList = new List<CommentMsgCtrl>();
		public static List<CommentMsgCtrl> commentCtrlList;
		private string mStrContent;
		private string lastContentCache;
		internal StackPanel LayoutRoot;
		internal MenuItem DeleteMsgMenu;
		internal Button ContentPanel;
		internal Image HeadImg;
		internal StackPanel Content1Panel;
		internal StackPanel NamePanel;
		internal EmojiText NickNameTxt;
		internal Image FromImg;
		internal SmileyText CommentEmojiTxt;
		internal Grid likeGrid;
		internal Image LikeIcon;
		internal TextBlock TimeTxt;
		internal Button detailGrid;
		internal Grid ImgGrid;
		internal Image SnsImage;
		internal Grid TxtGrid;
		internal RichTextBox SnsRichTxt;
		internal Paragraph sParagraph;
		private bool _contentLoaded;
		public CommentMsgCtrl()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.CommentMsgCtrl_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.CommentMsgCtrl_Unloaded));
			this.InitSource();
		}
		private void CommentMsgCtrl_Unloaded(object sender, RoutedEventArgs e)
		{
			CommentMsgCtrl.CommentHeadImgList.Remove(this);
			CommentMsgCtrl.DelCommentctrlToList(this);
		}
		private void CommentMsgCtrl_Loaded(object sender, RoutedEventArgs e)
		{
			CommentMsgCtrl.CommentHeadImgList.Add(this);
		}
		private void InitSource()
		{
			this.NickNameTxt.Foreground = R.theme.SNS_MSGPAGE_NICKNAME_BRUSH;
			this.CommentEmojiTxt.Foreground = R.theme.SNS_MSGPAGE_COMMENT_BRUSH;
			this.SnsRichTxt.set_Foreground(R.theme.SNS_MSGPAGE_DESC_BRUSH);
			this.TimeTxt.set_Foreground(R.theme.SNS_MSGPAGE_TIME_BRUSH);
		}
		private void DelItem_Click(object sender, RoutedEventArgs e)
		{
			StorageMgr.snsMsg.method_5(this.snsMsg.nMsgID);
		}
		public void RefreshHeadImg()
		{
			this.HeadImg.set_Source(HeadImgMgr.getHeadImage(this.snsMsg.strFromUsername, true));
		}
		protected override void ObjectCome(object object_0)
		{
			if (object_0 != null && object_0 is snsMediaDownloadInfo)
			{
				snsMediaDownloadInfo snsMediaDownloadInfo = object_0 as snsMediaDownloadInfo;
				if (snsMediaDownloadInfo.object_0 != null && snsMediaDownloadInfo.object_0 is BitmapImage)
				{
					this.SnsImage.set_Source(snsMediaDownloadInfo.object_0 as BitmapImage);
					this.btimgList = new List<BitmapImage>();
					this.btimgList.Add(snsMediaDownloadInfo.object_0 as BitmapImage);
				}
				return;
			}
		}
		public void Refresh(SnsMsg item)
		{
			this.snsMsg = item;
			Contact contact = StorageMgr.contact.method_0(item.strFromUsername);
			if (contact != null && ContactMgr.isContact(contact))
			{
				this.NickNameTxt.set_MaxWidth(232.0);
				this.FromImg.set_Visibility(1);
			}
			else
			{
				this.NickNameTxt.set_MaxWidth(198.0);
				this.FromImg.set_Visibility(0);
				if (item.nSource != 26u && item.nSource != 27u && item.nSource != 28u && item.nSource != 29u && item.nSource != 23u && item.nSource != 24u)
				{
					if (item.nSource != 22u)
					{
						if (item.nSource == 18u)
						{
							this.FromImg.set_Source(new ImageSourceConverter().ConvertFromString(R.theme.Sns_ImgPath_Comment_FromLbs) as ImageSource);
							goto IL_133;
						}
						this.FromImg.set_Source(new ImageSourceConverter().ConvertFromString(R.theme.Sns_ImgPath_Comment_FromIdontknow) as ImageSource);
						goto IL_133;
					}
				}
				this.FromImg.set_Source(new ImageSourceConverter().ConvertFromString(R.theme.Sns_ImgPath_Comment_FromShake) as ImageSource);
			}
			IL_133:
			this.HeadImg.set_Source(HeadImgMgr.getHeadImage(item.strFromUsername, true));
			this.NickNameTxt.Text = ContactHelper.getDisplayName(item.strFromUsername, null);
			this.TimeTxt.set_Text(Util.formatTimeInChatMsg((long)((ulong)item.nCreateTime)));
			this.snsInfo = StorageMgr.snsInfo.getByObjectID(item.strObjectID);
			if (this.snsInfo == null)
			{
				NetSceneSnsDetail netSceneSnsDetail = new NetSceneSnsDetail();
				netSceneSnsDetail.doScene(SnsInfo.toID(item.strObjectID));
				CommentMsgCtrl.AddCommentctrlToList(this);
			}
			if (this.snsInfo != null)
			{
				this.tlObj = SnsInfoMgr.getTimeLine(this.snsInfo);
			}
			else
			{
				this.tlObj = null;
			}
			if (this.tlObj != null)
			{
				if (this.tlObj.content.nStyle != 1)
				{
					if (this.tlObj.content.nStyle != 7)
					{
						if (this.tlObj.content.nStyle == 2)
						{
							this.ImgGrid.set_Visibility(1);
							this.TxtGrid.set_Visibility(0);
							this.mStrContent = this.tlObj.strContentDesc;
							this.RefreshContent();
							goto IL_3CC;
						}
						if (this.tlObj.content.nStyle == 3)
						{
							this.ImgGrid.set_Visibility(0);
							this.TxtGrid.set_Visibility(1);
							Panel arg_2AD_0 = this.ImgGrid;
							SolidColorBrush solidColorBrush = new SolidColorBrush();
							solidColorBrush.set_Color(Color.FromArgb(255, 37, 185, 212));
							arg_2AD_0.set_Background(solidColorBrush);
							this.SnsImage.set_Source(new ImageSourceConverter().ConvertFromString("/Source/Plugin/Sns/Common/Resource/News_icon.png") as ImageSource);
							this.SnsImage.set_Stretch(0);
							goto IL_3CC;
						}
						goto IL_3CC;
					}
				}
				this.TxtGrid.set_Visibility(1);
				this.ImgGrid.set_Visibility(0);
				snsMediaDownloadInfo snsMediaDownloadInfo = new snsMediaDownloadInfo
				{
					string_0 = this.tlObj.content.mediaList.get_Item(this.tlObj.content.mediaList.get_Count() - 1).strThumb,
					urlType = (SnsUrlType)this.tlObj.content.mediaList.get_Item(this.tlObj.content.mediaList.get_Count() - 1).nUrlType,
					snsObjID = SnsInfo.toID(item.strObjectID),
					priority = SnsPriority.LOW
				};
				base.StartRequest(snsMediaDownloadInfo, true);
				this.SnsImage.set_Stretch(3);
				if (snsMediaDownloadInfo.object_0 != null && snsMediaDownloadInfo.object_0 is BitmapImage)
				{
					this.SnsImage.set_Source(snsMediaDownloadInfo.object_0 as BitmapImage);
				}
				IL_3CC:
				if (item.nType != 1u)
				{
					if (item.nType != 5u)
					{
						if (item.nType != 2u)
						{
							if (item.nType != 3u)
							{
								this.likeGrid.set_Visibility(1);
								this.CommentEmojiTxt.set_Visibility(0);
								this.CommentEmojiTxt.Text = strings.WC_Comment_SendTo_You;
								return;
							}
						}
						this.likeGrid.set_Visibility(1);
						this.CommentEmojiTxt.set_Visibility(0);
						this.CommentEmojiTxt.Text = item.strContent;
						return;
					}
				}
				if (this.tlObj.content.nStyle != 1 && this.tlObj.content.nStyle != 2)
				{
					if (this.tlObj.content.nStyle != 3)
					{
						if (this.tlObj.content.nStyle == 7)
						{
							this.likeGrid.set_Visibility(1);
							this.CommentEmojiTxt.set_Visibility(0);
							this.CommentEmojiTxt.Text = strings.Album_Comment_LikeBG;
							return;
						}
						return;
					}
				}
				this.CommentEmojiTxt.set_Visibility(1);
				this.likeGrid.set_Visibility(0);
				this.LikeIcon.set_Source(new ImageSourceConverter().ConvertFromString(R.theme.Sns_ImgPath_Comment_like) as ImageSource);
				return;
			}
			this.TxtGrid.set_Visibility(1);
			this.ImgGrid.set_Visibility(1);
			if (item.nType != 2u)
			{
				if (item.nType != 3u)
				{
					if (item.nType == 4u)
					{
						this.likeGrid.set_Visibility(1);
						this.CommentEmojiTxt.set_Visibility(0);
						this.CommentEmojiTxt.Text = strings.WC_Comment_SendTo_You;
						return;
					}
					this.CommentEmojiTxt.set_Visibility(1);
					this.likeGrid.set_Visibility(0);
					this.LikeIcon.set_Source(new ImageSourceConverter().ConvertFromString(R.theme.Sns_ImgPath_Comment_like) as ImageSource);
					return;
				}
			}
			this.likeGrid.set_Visibility(1);
			this.CommentEmojiTxt.set_Visibility(0);
			this.CommentEmojiTxt.Text = item.strContent;
		}
		private void ContentPanel_Click(object sender, RoutedEventArgs e)
		{
			this.GoToDetailPage();
		}
		private void detailGrid_Click(object sender, RoutedEventArgs e)
		{
			if (this.snsInfo == null)
			{
				this.snsInfo = StorageMgr.snsInfo.getByObjectID(this.snsMsg.strObjectID);
				if (this.snsInfo == null)
				{
					MMMessageBox.Show(strings.Album_Comment_ContentBeenDeleted, true, "确定");
					return;
				}
			}
			if (this.tlObj == null)
			{
				this.tlObj = SnsInfoMgr.getTimeLine(this.snsInfo);
				if (this.tlObj == null)
				{
					return;
				}
			}
			if (this.tlObj.content.nStyle != 1)
			{
				if (this.tlObj.content.nStyle != 7)
				{
					Contact contact = StorageMgr.contact.method_0(this.snsMsg.strFromUsername);
					if (contact != null && ContactMgr.isContact(contact))
					{
						TimelineDetailPage.GotoThisPage(this.snsInfo, null);
						return;
					}
					List<SnsComment> msgByUserName = SnsMsgMgr.getMsgByUserName(this.snsMsg.strFromUsername, this.snsMsg.strObjectID, 0, 2147483647);
					TimelineDetailPage.GotoThisPage(this.snsInfo, msgByUserName);
					return;
				}
			}
			List<TloMedia> list = new List<TloMedia>();
			list.Add(this.tlObj.content.mediaList.get_Item(this.tlObj.content.mediaList.get_Count() - 1));
			List<Rect> list2 = new List<Rect>();
			list2.Add(ToolFunction.GetElementPos(this.SnsImage));
			ScrollPicsCtrlArgs args = new ScrollPicsCtrlArgs(list, this.btimgList, list2, 0, null, null, null);
			ScrollPicListPage.GotoThisPage(args, null);
		}
		private void GoToDetailPage()
		{
			if (this.snsInfo == null)
			{
				this.snsInfo = StorageMgr.snsInfo.getByObjectID(this.snsMsg.strObjectID);
				if (this.snsInfo == null)
				{
					MMMessageBox.Show(strings.Album_Comment_ContentBeenDeleted, true, "确定");
					return;
				}
			}
			Contact contact = StorageMgr.contact.method_0(this.snsMsg.strFromUsername);
			if (contact != null && ContactMgr.isContact(contact))
			{
				TimelineDetailPage.GotoThisPage(this.snsInfo, null);
				return;
			}
			List<SnsComment> msgByUserName = SnsMsgMgr.getMsgByUserName(this.snsMsg.strFromUsername, this.snsMsg.strObjectID, 0, 2147483647);
			TimelineDetailPage.GotoThisPage(this.snsInfo, msgByUserName);
		}
		public static void RefreshImage(List<string> userNameList)
		{
			for (int i = 0; i < userNameList.get_Count(); i++)
			{
				for (int j = 0; j < CommentMsgCtrl.CommentHeadImgList.get_Count(); j++)
				{
					if (userNameList.get_Item(i) == CommentMsgCtrl.CommentHeadImgList.get_Item(j).snsMsg.strFromUsername)
					{
						CommentMsgCtrl.CommentHeadImgList.get_Item(j).RefreshHeadImg();
						CommentMsgCtrl.CommentHeadImgList.RemoveAt(j);
					}
				}
			}
		}
		public static void AddCommentctrlToList(CommentMsgCtrl nowControl)
		{
			if (CommentMsgCtrl.commentCtrlList == null)
			{
				CommentMsgCtrl.commentCtrlList = new List<CommentMsgCtrl>();
			}
			CommentMsgCtrl.commentCtrlList.Add(nowControl);
		}
		public static void DelCommentctrlToList(CommentMsgCtrl nowControl)
		{
			if (CommentMsgCtrl.commentCtrlList == null)
			{
				return;
			}
			CommentMsgCtrl.commentCtrlList.Remove(nowControl);
		}
		public static void RefreshUI(string strObjId)
		{
			if (CommentMsgCtrl.commentCtrlList == null)
			{
				return;
			}
			for (int i = 0; i < CommentMsgCtrl.commentCtrlList.get_Count(); i++)
			{
				if (CommentMsgCtrl.commentCtrlList.get_Item(i).snsMsg.strObjectID == strObjId)
				{
					CommentMsgCtrl.commentCtrlList.get_Item(i).Refresh(CommentMsgCtrl.commentCtrlList.get_Item(i).snsMsg);
				}
			}
		}
		private void RefreshContent()
		{
			if (this.sParagraph == null)
			{
				return;
			}
			if (string.IsNullOrEmpty(this.mStrContent))
			{
				return;
			}
			if (this.mStrContent == this.lastContentCache)
			{
				return;
			}
			this.lastContentCache = this.mStrContent;
			this.sParagraph.get_Inlines().Clear();
			List<InLineResult> inLineResult = RichTextHelper.GetInLineResult(this.mStrContent, (int)this.SnsRichTxt.get_FontSize(), null, null);
			int num = 0;
			using (List<InLineResult>.Enumerator enumerator = inLineResult.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					InLineResult current = enumerator.get_Current();
					if (current.mInline != null)
					{
						num++;
						this.sParagraph.get_Inlines().Add(current.mInline);
						if (num >= 400)
						{
							Run run = new Run();
							run.set_Text("......");
							this.sParagraph.get_Inlines().Add(run);
							break;
						}
					}
				}
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Common/UI/SnsMsg/CommentMsgCtrl.xaml", 2));
			this.LayoutRoot = (StackPanel)base.FindName("LayoutRoot");
			this.DeleteMsgMenu = (MenuItem)base.FindName("DeleteMsgMenu");
			this.ContentPanel = (Button)base.FindName("ContentPanel");
			this.HeadImg = (Image)base.FindName("HeadImg");
			this.Content1Panel = (StackPanel)base.FindName("Content1Panel");
			this.NamePanel = (StackPanel)base.FindName("NamePanel");
			this.NickNameTxt = (EmojiText)base.FindName("NickNameTxt");
			this.FromImg = (Image)base.FindName("FromImg");
			this.CommentEmojiTxt = (SmileyText)base.FindName("CommentEmojiTxt");
			this.likeGrid = (Grid)base.FindName("likeGrid");
			this.LikeIcon = (Image)base.FindName("LikeIcon");
			this.TimeTxt = (TextBlock)base.FindName("TimeTxt");
			this.detailGrid = (Button)base.FindName("detailGrid");
			this.ImgGrid = (Grid)base.FindName("ImgGrid");
			this.SnsImage = (Image)base.FindName("SnsImage");
			this.TxtGrid = (Grid)base.FindName("TxtGrid");
			this.SnsRichTxt = (RichTextBox)base.FindName("SnsRichTxt");
			this.sParagraph = (Paragraph)base.FindName("sParagraph");
		}
	}
}
